﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WpfApp_Property
{
    public class CurtomPanel:Panel
    {
        public double ColumnSpan
        {
            get { return (double)GetValue(ColumnSpanPropertyProperty); }
            set { SetValue(ColumnSpanPropertyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ColumnSpanProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ColumnSpanPropertyProperty =
            DependencyProperty.Register("ColumnSpanProperty", typeof(double), typeof(CurtomPanel), new PropertyMetadata(0.0,new PropertyChangedCallback(ColumnSpanChange)));



        public double RowSpan
        {
            get { return (double)GetValue(RowSpanProperty); }
            set { SetValue(RowSpanProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RowSpan.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RowSpanProperty =
            DependencyProperty.Register("RowSpan", typeof(double), typeof(CurtomPanel), 
                new FrameworkPropertyMetadata(0.0,FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsArrange));

        static void ColumnSpanChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as CurtomPanel)!.InvalidateVisual();
        }


        //测量
        protected override Size MeasureOverride(Size availableSize)
        {
            double _total_height = 0;
            double _width = (availableSize.Width - ColumnSpan * 2) / 3;
            double _maxHeight = 0;
            for (int i = 0; i < this.InternalChildren.Count; i++)
            {

                UIElement element = this.InternalChildren[i];
                element.Measure(new Size(_width, double.MaxValue));
                _maxHeight = Math.Max(element.DesiredSize.Height, _maxHeight);
                if (i % 3 == 0)
                {
                    _total_height += _maxHeight;
                    _total_height += RowSpan;
                    _maxHeight = 0;
                }
            }

            return new Size(availableSize.Width, _total_height);
        }

        //排列
        protected override Size ArrangeOverride(Size finalSize)
        {
            double offset_y = 0;
            double offset_x = 0;
            double _width = (finalSize.Width - ColumnSpan * 2) / 3;
            double _maxHeight = 0;
            for (int i = 1; i <= this.InternalChildren.Count; i++)
            {
                UIElement element = this.InternalChildren[i - 1];
                element.Arrange(new Rect(offset_x, offset_y, _width, element.DesiredSize.Height));
                _maxHeight = Math.Max(element.DesiredSize.Height, _maxHeight);
                if (i % 3 == 0)
                {
                    offset_y += _maxHeight;
                    offset_y += RowSpan;
                    offset_x = 0;
                    _maxHeight = 0;
                }
                else
                {
                    offset_x += _width;
                    offset_x += ColumnSpan;

                }

            }
            return base.ArrangeOverride(finalSize);
        }
    }
}
